﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core.Querying;

namespace Medianamik.Core.DAL.SQL
{
    public class Predicate : IPredicate, ISqlQueryPart
    {
        public string Subject { get; private set; }
        public Comparison Comparison { get; private set; }

        string IPredicate.Operator
        {
            get
            {
                return this.Comparison.ToSqlOperator();
            }
        }

        private IExpression _expression;
        public IExpression Expression
        {
            get
            {
                if (_expression == null)
                {
                    throw new ArgumentNullException("Expression");
                }
                return _expression;
            }
        }

        public Predicate(string pSubject, Comparison pComparison, IExpression pExpression )
        {
            Subject = pSubject;
            Comparison = pComparison;
            _expression = pExpression;
        }

        public virtual StringBuilder ToSQL()
        {
            var sql= new StringBuilder()
            .Append(Subject)
            .Append(Comparison.ToSqlOperator() )
            .Append(" " + Expression.ToSQL());

            return sql;
        }

    }
}
